package com.zlx.guguanjia.controller;

import com.baomidou.kaptcha.exception.KaptchaException;
import com.baomidou.kaptcha.exception.KaptchaIncorrectException;
import com.baomidou.kaptcha.exception.KaptchaNotFoundException;
import com.baomidou.kaptcha.exception.KaptchaTimeoutException;
import com.zlx.guguanjia.entity.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author 周罗校
 * @Description
 * @ClassName ExceptionController
 * @date 2020.11.03 10:32:25
 * 该版权归周罗校所有，最终解释权以官方为准
 */
@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler(value = KaptchaException.class)
    @ResponseBody
    public Result kaptchaExceptionHandler(KaptchaException kaptchaException) {
        String message = null;
        if (kaptchaException instanceof KaptchaIncorrectException) {
            message = "验证码不正确";
        } else if (kaptchaException instanceof KaptchaNotFoundException) {
            message = "验证码未找到";
        } else if (kaptchaException instanceof KaptchaTimeoutException) {
            message = "验证码过期";
        } else {
            message = "验证码渲染失败";
        }
        return new Result(false,message,null);
    }
}
